﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OverrideErrorPages
{
    public class OverrideErrorPages : IPlugin.IPlugin
    {
        #region InfoPlugin

        public string[] Authors
        {
            get
            {
                return new string[] { "Manuel Fernandez" };
            }
        }

        public string Name
        {
            get
            {
                return "OverrideErrorPages";
            }
        }

        public string Description
        {
            get
            {
                return "Overwrite error pages (based on the response code error) from server with personalized error pages";
            }
        }

        public IPlugin.PluginRing Ring
        {
            get
            {
                return IPlugin.PluginRing.Ring0;
            }
        }

        #endregion

        public Model.Elements.HttpPacket Start(Model.Elements.HttpPacket packet)
        {
            if (packet is Model.Elements.HttpResponsePacket)
            {
                Model.Elements.HttpResponsePacket response = packet as Model.Elements.HttpResponsePacket;
                
                if (response.errorCode == "404")
                {
                    Model.Elements.HttpResponsePacket newResponse = IPlugin.API.General.ResponseByErrorCode(response.errorCode);
                    newResponse.SetRequestPacket(response.GetRequestPacket());

                    return newResponse;
                }

                if (response.errorCode == "500")
                {
                    Model.Elements.HttpResponsePacket newResponse = IPlugin.API.General.ResponseByErrorCode(response.errorCode);
                    newResponse.SetRequestPacket(response.GetRequestPacket());
                    return newResponse;
                }
            }

            return packet;
        }
    }
}
